package com.ddx.service.impl;

import com.ddx.common.enums.ErrorCode;
import com.ddx.common.exception.BusinessException;
import com.ddx.common.exception.ResultModel;
import com.ddx.dao.UserFollowMapper;
import com.ddx.pojo.UserFollow;
import com.ddx.service.ProvideIdeaService;
import com.ddx.service.UserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * xxx
 * Created by Administrator on 2017/5/8.
 * Administrator
 */
@Service
public class UserFollowServiceImpl  implements UserFollowService {

    @Autowired
    UserFollowMapper userFollowMapper;

    @Override
    public List<UserFollow> getList4userId(Integer userId) throws Exception {
        return userFollowMapper.getList4userId(userId);
    }

    @Override
    public void follow(String userId, String followId) {
        if (Objects.isNull(userId) || Objects.isNull(followId)) {
            throw new BusinessException(ErrorCode.BUSSINESS_ERROR_201);
        }
        UserFollow record=userFollowMapper.selectByUserIdAndFollowId(Integer.parseInt(userId),Integer.parseInt(followId));
        if(record!=null){
            userFollowMapper.deleteByPrimaryKey(record.getId());
        }else {
            UserFollow userFollow = new UserFollow();
            userFollow.setUserId(Integer.parseInt(userId));
            userFollow.setFollowId(Integer.parseInt(followId));
            userFollowMapper.insertSelective(userFollow);
        }
    }
}